# Maintainer: Peter Budai <peterbudai@hotmail.com>

_realname=dlib
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=19.20
pkgrel=1
pkgdesc="A toolkit for making real world machine learning and data analysis applications in C++ (mingw-w64)"
arch=('any')
url='http://dlib.net/'
license=('Boost')
depends=("${MINGW_PACKAGE_PREFIX}-lapack"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-lapack"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('strip')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/davisking/${_realname}/archive/v${pkgver}.tar.gz"
        "001-python-binding.patch"
        "003-proper-link-libraries.patch")
sha256sums=('fc3f0986350e8e53aceadf95a71d2f413f1eedc469abda99a462cb528741d411'
            'd40f90a70da30689343071a7194ddcff73e225a6fff49ef6389b3dd861ac586f'
            'b330f72fe0b679f9245d7ef9b0780ed0712a8806d325cb072afdb02ffdba3196')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/001-python-binding.patch"
  patch -Np1 -i "${srcdir}/003-proper-link-libraries.patch"

  cd "${srcdir}"
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  cd "$srcdir"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DUSE_AVX_INSTRUCTIONS=ON \
      -DDLIB_ISO_CPP_ONLY=OFF \
      -DDLIB_IN_PROJECT_BUILD=OFF \
      -DDLIB_USE_CUDA=OFF \
      -DDLIB_JPEG_SUPPORT=ON \
      -DDLIB_PNG_SUPPORT=ON \
      -DDLIB_GIF_SUPPORT=ON \
      -DDLIB_LINK_WITH_SQLITE3=ON \
      -DDLIB_USE_FFTW=ON \
      -DBUILD_SHARED_LIBS=ON \
      ../${_realname}-${pkgver}
  cmake --build . --config Release

  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/python setup.py build -G "MSYS Makefiles" \
    --set CMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    --set USE_AVX_INSTRUCTIONS=ON \
    --set DLIB_ISO_CPP_ONLY=OFF \
    --set DLIB_IN_PROJECT_BUILD=ON \
    --set DLIB_USE_CUDA=OFF \
    --set DLIB_JPEG_SUPPORT=ON \
    --set DLIB_PNG_SUPPORT=ON \
    --set DLIB_GIF_SUPPORT=ON \
    --set DLIB_LINK_WITH_SQLITE3=ON \
    --set DLIB_USE_FFTW=ON
}

#check() {
  #cd "${srcdir}"/build-${CARCH}
  #make check
#}

package_dlib() {
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-dlib")
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"

  # Remove hard coded library path from cmake files
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  for pcfile in  "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
  pushd "${pkgdir}${MINGW_PREFIX}/lib/cmake/dlib" > /dev/null
  sed -s "s|${PREFIX_DEPS}/include||g" -i ./dlibConfig.cmake
  sed -s -e "s|${PREFIX_DEPS}/lib/||g" -e "s|${PREFIX_DEPS}/include||g" -i ./dlib.cmake
  popd > /dev/null
}

package_python-dlib() {
  depends=("${MINGW_PACKAGE_PREFIX}-dlib"
           "${MINGW_PACKAGE_PREFIX}-python")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-numpy: Python interface")
  provides=("${MINGW_PACKAGE_PREFIX}-python3-dlib")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-dlib")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3-dlib")

  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1 --skip-build
}

package_mingw-w64-i686-dlib() {
  package_dlib
}

package_mingw-w64-x86_64-dlib() {
  package_dlib
}

package_mingw-w64-i686-python-dlib() {
  package_python-dlib
}

package_mingw-w64-x86_64-python-dlib() {
  package_python-dlib
}
